package com.atguigu.dga.governance.assess;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceMetric;
import com.atguigu.dga.meta.bean.TableMetaInfo;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

public abstract class  Assessor {

    public  final GovernanceAssessDetail metricAssess(AssessParam assessParam) {

        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        String assessDate = assessParam.getAssessDate();
        // 由父类进行控制 实现公共且不变行为
        //  创建考评结果对象
        GovernanceAssessDetail governanceAssessDetail=new GovernanceAssessDetail();
        //  填写基本信息  各种名称id  时间  给默认分数10
        //  负责处理异常
        governanceAssessDetail.setTableName(tableMetaInfo.getTableName());
        governanceAssessDetail.setSchemaName(tableMetaInfo.getSchemaName());
        governanceAssessDetail.setMetricId(governanceMetric.getId()+"");
        governanceAssessDetail.setMetricName(governanceMetric.getMetricName());
        governanceAssessDetail.setAssessDate(assessDate);
        governanceAssessDetail.setTecOwner(tableMetaInfo.getTableMetaInfoExtra().getTecOwnerUserName());
        governanceAssessDetail.setCreateTime(new Date());
        governanceAssessDetail.setGovernanceType(governanceMetric.getGovernanceType());
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);
        //   调用子类检查问题   子类检查是否有问题 ，如果有 给低分 ，给问题原因
        String skipAssessTables = assessParam.getGovernanceMetric().getSkipAssessTables();

        //   跳过考评表
        if(skipAssessTables!=null &&skipAssessTables.trim().length()>0){
            String[] skipTableArr = skipAssessTables.split(",");
            for (String skipTable : skipTableArr) {
                if(tableMetaInfo.getTableName().equals(skipTable)){
                    return governanceAssessDetail;
                }
            }
        }




        try {
           checkProblem(governanceAssessDetail,assessParam);
       }catch (Exception e){
           governanceAssessDetail.setIsAssessException("1");

           StringWriter stringWriter = new StringWriter(); //纸
           PrintWriter printWriter = new PrintWriter(stringWriter); //笔
           e.printStackTrace(printWriter);

           int limit = Math.min(stringWriter.toString().length(),2000) ;
           String exceptionMsg = stringWriter.toString().substring(0, limit);
           governanceAssessDetail.setAssessExceptionMsg(exceptionMsg);

       }

        //   返回考评结果明细
        return governanceAssessDetail;


    }


    public  abstract  void  checkProblem(GovernanceAssessDetail governanceAssessDetail,AssessParam assessParam) throws Exception;
}
